package utils;

import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;

public class CacheUtils {
	public static String HOME_PAGE_CACHE = "homePageCache";
	public static String CHAPTER_CACHE = "chapterCache";
	public static String DOWN_CACHE = "downCache";
	public static String BLOG_CACHE = "blogCache";
	public static String CODE_CACHE = "codeCache";
	public static String PROJ_CACHE = "projCache";
	public static String COURSE_CACHE = "courseCache";
	/**
	 * 获取cache，没有返回null
	 * */
	public static Cache getHomePageCache(String cacheName){
		Cache homeCache = CacheManager.getInstance().getCache(cacheName);
		return homeCache;
	}
	
	/**
	 * 获取某个cache内某个key的数据
	 * 存在则取出，否则返回null
	 * */
	public static Object getEleByNameAndKey(String cacheName,String key){
		Cache cache = CacheManager.getInstance().getCache(cacheName);
		if(cache != null){
			if( cache.get(key)!= null){
				return  cache.get(key).getObjectValue();
			}
		} 
		return null;
	}
	
	/**
	 * 存储cache
	 * */
	public static void addEleByParam(String cacheName,String key,Object obj){
		Element ele = new Element(key, obj);
		Cache cache = CacheManager.getInstance().getCache(cacheName);
		if(cache != null){
			 cache.put(ele);
		}
	}
	
	
}
